package com.sunlg.smarthome.ui.light;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.LightContract;
import com.sunlg.smarthome.contract.presenter.LightPresenter;
import com.sunlg.smarthome.data.entity.Light;
import com.sunlg.smarthome.ui.wedget.RecyclerViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LightListActivity extends BaseActivity
        implements LightContract.LightListView, AddLightDialog.OnEditTextInputListener{

    @BindView(R.id.rv_light_list)
    RecyclerView rvLightList;
    @BindView(R.id.tv_none_device)
    TextView tvNoneDevice;

    LightContract.Presenter presenter;
    private LightAdapter lightAdapter;
    private List<Light> lightList;
    private LinearLayoutManager layoutManager;
    private BroadcastReceiver mLightStatusChangedBr;


    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_light_list;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("照明设备");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        presenter = new LightPresenter();
        presenter.attachView(this);

        lightList = new ArrayList<>();
        lightAdapter = new LightAdapter(this, lightList);

        layoutManager = new LinearLayoutManager(this);
    }

    @Override
    protected void initView() {
        rvLightList.setAdapter(lightAdapter);
        rvLightList.setLayoutManager(layoutManager);
        rvLightList.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));

        presenter.loadAll();

        //注册灯光状态变化广播接收器
        mLightStatusChangedBr = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //改变状态
                Bundle bundle = intent.getExtras();
                int status = bundle.getInt("status");
                System.out.println(status);
                int pos = bundle.getInt("pos");
                Light light = lightList.remove(pos);
                light.setStatus(status);
                lightList.add(pos, light);

                //lightAdapter.notifyDataSetChanged();
                lightAdapter.notifyItemChanged(pos);
            }
        };
        IntentFilter intentFilter = new IntentFilter(C.Action.LIGHT_STATUS_CHANGED);
        registerReceiver(mLightStatusChangedBr, intentFilter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_add){
            AddLightDialog dialog = new AddLightDialog(this);
            dialog.setOnEditTextInputListener(this);
            dialog.show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onEditTextInput(Integer no, String inputText) {
        Light light = new Light();
        light.setDevice(no);
        light.setDescription(inputText);
        presenter.addLight(light);
    }


    @Override
    public void showNoneLightText() {
        if(tvNoneDevice.getVisibility() != View.VISIBLE){
            tvNoneDevice.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideNoneLightText() {
        if(tvNoneDevice.getVisibility() == View.VISIBLE){
            tvNoneDevice.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showLightList(List<Light> lightList) {
        this.lightList.clear();
        this.lightList.addAll(lightList);
        this.lightAdapter.notifyDataSetChanged();
    }

    @Override
    public void takeOnSuccess() {

    }

    @Override
    public void takeOffSuccess() {

    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
        unregisterReceiver(mLightStatusChangedBr);
    }

}
